package web;



import java.sql.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import model.Categoriearret;
import model.Descriptionarret;
import model.Ligne;
import model.Operateur;
import model.Stock;
import util.HibernateUtil;

public class Operation {
	//*******************Operations************************
	public void ajoutSt(Stock s){
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.save(s);
			session.getTransaction().commit();
			session.close();
	}
	public Stock updateStock(Stock s){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		System.err.println("session"+session);
		session.beginTransaction();
		session.update(s);
		session.getTransaction().commit();
		session.close();
		return s;
	}
	public void deleteStock(Stock s){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		System.err.println("session"+session);
		session.beginTransaction();
		session.delete(s);
		session.getTransaction().commit();
		session.close();
	}
	public Stock stockId(String stId){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		System.err.println("session"+session);
		session.beginTransaction();
		Stock s = (Stock) session.load(Stock.class, stId);
		return s;
	}
	@SuppressWarnings("unchecked")
	public List<Stock> getStockById(String stId) throws HibernateException{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		System.err.println("session"+session);
		session.beginTransaction();
		
		return session.createQuery("from Stock st where st.date=\'"+stId+"\'").list();
	}
	@SuppressWarnings("unchecked")
	public List<Stock> allDateStock() throws HibernateException{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		System.err.println("session"+session);
		session.beginTransaction();
		
		return session.createQuery("select date from Stock").list();
	}
	@SuppressWarnings("unchecked")
	public List<Stock> allStock() throws HibernateException{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		System.err.println("session"+session);
		session.beginTransaction();
		
		return session.createQuery("from Stock order by date").list();
	}
	//*******************Operations sur les operateurs
	public void ajoutOperateur(Operateur op){
	    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.save(op);
        session.getTransaction().commit();
        session.close();
        }
	public Operateur updateOperateur(Operateur op)  throws HibernateException {
	
	   Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        System.err.println("session"+session);
        session.beginTransaction();
        session.update(op);
        session.getTransaction().commit();
        session.close();
        return op;
        }
	public void deleteOperateur(Operateur op) throws HibernateException{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		System.err.println("session"+session);
		session.beginTransaction();
		session.delete(op);
		session.getTransaction().commit();
		session.close();
		}
	public Operateur OperateurId(String id) throws HibernateException{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	    System.err.println("session"+session);
	    session.beginTransaction();
	    Operateur op = (Operateur) session.load(Operateur.class, id);
		
		return op;
	}
	@SuppressWarnings("unchecked")
	public List<Operateur> getOperateurById(String IdOp) throws HibernateException{
		
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		System.err.println("session"+session);
		session.beginTransaction();
		
		return session.createQuery("from Operateur opr where opr.idoperateur="+IdOp).list();
	}
	
	@SuppressWarnings("unchecked")
	public List<Operateur> allOperateur() throws HibernateException{
		
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		System.err.println("session"+session);
		session.beginTransaction();
		
		//dans cette methode on utilise le language "sql" de hibernate s'appelant HQL 
		return session.createQuery("from Operateur").list();
}
	public boolean login(Operateur opr) {
		// TODO Auto-generated method stub

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		System.err.println("session"+session);
		session.beginTransaction();
		String hql = "from Operateur as O where O.loginop =:log and O.passop=:pas and O.isadmin='true' ";
		Query query = session.createQuery(hql);
		query.setParameter("log",opr.getLoginop());
		query.setParameter("pas",opr.getPassop());
		List results = query.list();
		return results.size() > 0 ? true : false;
	}
	public Operateur opera(Operateur opr) {
		// TODO Auto-generated method stub

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		System.err.println("session"+session);
		session.beginTransaction();
		String hql = "from Operateur as O where O.loginop =:log and O.passop=:pas ";
		Query query = session.createQuery(hql);
		query.setParameter("log",opr.getLoginop());
		query.setParameter("pas",opr.getPassop());
		List results = query.list();
		return results.size() > 0  ? (Operateur)results.get(0) : null ;
	}
	
	//*******************Operations sur les Lignes************************
	public void ajoutLi(Ligne li){
		    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	        session.beginTransaction();
	        session.save(li);
	        session.getTransaction().commit();
	        session.close();
	}
	 
	public Ligne updateLigne(Ligne li)  throws HibernateException {
		
		   Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	        System.err.println("session"+session);
	        session.beginTransaction();
	        session.update(li);
	        session.getTransaction().commit();
	        session.close();
	        return li;
	}
	
	public void deleteLi(Ligne li) throws HibernateException{
	    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        System.err.println("session"+session);
        session.beginTransaction();
        session.delete(li);
        session.getTransaction().commit();
        session.close();
        }
        
	
	
		public Ligne ligneId(String id) throws HibernateException{
			
			    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		        System.err.println("session"+session);
		        session.beginTransaction();
		        Ligne li = (Ligne) session.load(Ligne.class, id);
			
			return li;
		}
		@SuppressWarnings("unchecked")
		public List<Ligne> getLigneById(String IdLi) throws HibernateException{
			
			
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			System.err.println("session"+session);
			session.beginTransaction();
			
			return session.createQuery("from Ligne lig where lig.idligne="+IdLi).list();
			
		}
		
		@SuppressWarnings("unchecked")
		public List<Ligne> allLigne() throws HibernateException{
			
			
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			System.err.println("session"+session);
			session.beginTransaction();
			
			//dans cette methode on utilise le language "sql" de hibernate s'appelant HQL 
			return session.createQuery("from Ligne").list();
	}
		
		//*******************Operations sur les Categories************************
		
		
		public void ajoutCat(Categoriearret cat){
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.save(cat);
			session.getTransaction().commit();
			session.close();
		}
		
		public Categoriearret updateCat(Categoriearret cat) throws HibernateException{
			
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			System.err.println("session"+session);
			session.beginTransaction();
			session.update(cat);
			session.getTransaction().commit();
			session.close();
			return cat;
			
		}
		
		public void deleteCat(Categoriearret cat) throws HibernateException{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			System.err.println("session"+session);
			session.beginTransaction();
			session.delete(cat);
			session.getTransaction().commit();
			session.close();
			
		}
		public Categoriearret categorieId(String idCat) throws HibernateException{
			
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			System.err.println("session"+session);
			session.beginTransaction();
			Categoriearret cat = (Categoriearret) session.load(Categoriearret.class, idCat);
			
			return cat;
		}
		@SuppressWarnings("unchecked")
		public List<Categoriearret> getCategorieById(String IdCat) throws HibernateException{
			
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			System.err.println("session"+session);
			session.beginTransaction();
			return session.createQuery("from Categoriearret cat where cat.idfam="+IdCat).list();
		}
		@SuppressWarnings("unchecked")
		public List<Categoriearret> getAllIdCategorie() throws HibernateException{
			
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			System.err.println("session"+session);
			session.beginTransaction();
			return session.createQuery("select categorie from Stock").list();
		}
		@SuppressWarnings("unchecked")
		public List<Categoriearret> allCategorie() throws HibernateException{
			
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			System.err.println("session"+session);
			session.beginTransaction();
			
			return session.createQuery("from Categoriearret").list();
			
		}
		
		//*******************Operations sur les Arrêts************************
		
		
				public void ajoutArr(Descriptionarret arr){
					Session session = HibernateUtil.getSessionFactory().getCurrentSession();
					session.beginTransaction();
					session.save(arr);
					session.getTransaction().commit();
					session.close();
				}
				
				public Descriptionarret updateArr(Descriptionarret arr) throws HibernateException{
					
					Session session = HibernateUtil.getSessionFactory().getCurrentSession();
					System.err.println("session"+session);
					session.beginTransaction();
					session.update(arr);
					session.getTransaction().commit();
					session.close();
					return arr;
					
				}
				
				public void deleteArr(Descriptionarret arr) throws HibernateException{
					Session session = HibernateUtil.getSessionFactory().getCurrentSession();
					System.err.println("session"+session);
					session.beginTransaction();
					session.delete(arr);
					session.getTransaction().commit();
					session.close();
					
				}
				public Descriptionarret ArretId(String idArr) throws HibernateException{
					
					Session session = HibernateUtil.getSessionFactory().getCurrentSession();
					System.err.println("session"+session);
					session.beginTransaction();
					Descriptionarret arr = (Descriptionarret) session.load(Descriptionarret.class, idArr);
					
					return arr;
				}
				@SuppressWarnings("unchecked")
				public List<Descriptionarret> getArretById(String IdArr) throws HibernateException{
					
					Session session = HibernateUtil.getSessionFactory().getCurrentSession();
					System.err.println("session"+session);
					session.beginTransaction();
					return session.createQuery("from Descriptionarret arr where arr.idarret="+IdArr).list();
				}
				@SuppressWarnings("unchecked")
				public List<Descriptionarret> allArret() throws HibernateException{
					
					Session session = HibernateUtil.getSessionFactory().getCurrentSession();
					System.err.println("session"+session);
					session.beginTransaction();
					
					return session.createQuery("from Descriptionarret").list();					
				}
				@SuppressWarnings("unchecked")
				public List<Descriptionarret> getArretByCat(String idCat) throws HibernateException{
					
					Session session = HibernateUtil.getSessionFactory().getCurrentSession();
					System.err.println("session"+session);
					session.beginTransaction();
					return session.createQuery("from Descriptionarret arr where arr.categorie="+idCat).list();
				}
			
}
